<?php 
class MY_Model extends CI_Model{
	protected $_table_name='tbl_topics';
	protected $_primary_key='topic_id';
	protected $_primary_filter='intval';
	protected $_order_by='';
	public  $_rules='';
	protected $_timestamps=false;
	
	function __constructor(){
		parent::__constructor();
	}
	public function get($id=null, $single=FALSE){
		if($id!=null){
			$filter =$this->_primary_filter;
			$id= $filter($id);
			$this->db->where($this->_primary_key,$id);
			$method ='row';
				
		}elseif($single==TRUE){
				$method='row';
		}else{
			$method='result';
		}
		/*if(!count($this->db->ar_orderby)){
			$this->db->order_by($this->_order_by);
		}*/
		return $this->db->get($this->_table_name)->$method();
		
	}
	
	public function get_by($where, $single=FALSE){
		$this->db->where($where);
		return $this->get(null,$single);
		
	}
	public function save($data,$id=null){
		// Set timestamp
		if($this->_timestamps==TRUE){
			$now=date('Y-m-d H:i:s');
			$id||$data['created']=$now;
			$data['modified']=$now;
		}
		// Insert
		if($id==null){
			!isset($data[$this->_primary_key])|| $data[$this->_primary_key]=null;
			$this->db->set($data);
			$this->db->insert($this->_table_name);
			$id=$this->db->insert_id();
		}
		//Update
		else{
			$filter=$this->_primary_filter;
			$id=$filter($id);
			$this->db->set($data);
			$this->db->where($this->_primary_key,$id);
			$this->db->update($this->_table_name);	
		}
		return $id;
	}
	public function delete($id){
		$filter =$this->_primary_filter;
		$id=$filter($id);
		if(!$id){
			return FALSE;
		}
		$this->db->where($this->_primary_key,$id);
		$this->db->limit(1);
		$this->db->delete($this->_table_name);
	}
}
?>
